home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_5 / a5_1.m next >
Encoding:
Text File  |  1994-06-05  |  2.3 KB  |  95 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 5.1 (Least Squares Line).
  9. % Section    5.1,    Least-Squares Line, Page 264
  10. echo on; clc; format short; hold off; clear
  11.  
  12. % This program finds the least squares line, given a set of
  13.  
  14. % data points { (x , y ), (x , y ) ,..., (x , y ) }.
  15. %                 1   1     2   2          n   n
  16.  
  17. % The abscissas and ordinates are stored in X and Y, respectively.
  18.  
  19. % X = [x , x  ,..., x ];  Y = [y , y  ,..., y ];
  20. %       1   2        n          1   2        n
  21.  
  22. % Remark. lsline.m is used for Algorithm 5.1
  23.  
  24. pause % Press any key to continue.
  25.  
  26. clc;
  27. %    Place the abscissas for the points in  X.
  28.  
  29. %    Place the ordinates for the points in  Y.
  30.  
  31. X = [-1  0  1  2  3  4  5  6];
  32.  
  33. Y = [10  9  7  5  4  3  0 -1];
  34.  
  35. [A B] = lsline(X,Y);
  36.  
  37. pause    % Press any key to graph data points.
  38.  
  39. clc; clg;
  40. a = min(X)-0.20; b = max(X)+0.20;
  41. c = min(Y)-0.35; d = max(Y)+0.35;
  42. axis([a b c d]);...
  43. plot(X,Y,'or');...
  44. hold on;...
  45. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  46. xlabel('x');...
  47. ylabel('y');...
  48. title('The given x-y data points.');...
  49. grid;...
  50. axis;...
  51. hold off;...
  52. shg; pause    % Press any key to continue.
  53.  
  54. points = [X;Y]; format short;
  55. clc,disp(''),disp('The given x-y data points.'),...
  56. disp('     x     y'),disp(points'),...
  57.  
  58. pause    % Press any key to continue.
  59.  
  60. clc; clg;
  61. a = -1;
  62. b = 7;
  63. c = -2;
  64. d = 12;
  65. Xs = [a b];
  66. Ys = A*Xs + B;
  67. axis([a b c d]);...
  68. plot(X,Y,'or',Xs,Ys,'-g');...
  69. hold on;...
  70. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  71. xlabel('x');...
  72. ylabel('y');...
  73. Mx1 = 'Least squares line: f(x) = ';...
  74. Mx2 = [Mx1,num2str(A),' x'];...
  75. if B > 0,
  76.   Mx3 = [Mx2,' + ',num2str(B)];
  77. else
  78.   Mx3 = [Mx2,' - ',num2str(abs(B))];
  79. end;...
  80. title(Mx3);...
  81. grid;...
  82. axis;...
  83. hold off;...
  84. shg; pause    % Press any key to continue.
  85.  
  86. Mx4 = 'The given x-y data points.';
  87. clc,echo off,diary output,...
  88. disp(''),disp(Mx3),disp(Mx4),...
  89. disp('     x     y'),disp(points'),diary off, echo on
  90. pause    % Press any key to continue.
  91. points = [X;Y;A*X+B;Y-(A*X+B)]';
  92. Mx5='    x(k)      y(k)      f(x(k))   error';
  93. clc,echo off,diary output,disp(''),disp(Mx3),...
  94. disp(''),disp(Mx5),disp(points),diary off,echo on
  95.